package com.example.finalapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ProgressBar;

import java.util.List;

public class FoodActivity extends AppCompatActivity {
    private static final String TAG = "FoodActivity";

    private Handler handler;
    private ProgressBar bar;
    private ArrayAdapter adapter;
    private GridView mylist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);

        mylist = findViewById(R.id.mylistview);
        bar = findViewById(R.id.progressBar);
        mylist.setEmptyView(bar);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if (msg.what==5){
                    List<String> list = (List<String>) msg.obj;
                    adapter = new ArrayAdapter<String>(FoodActivity.this,
                            android.R.layout.simple_list_item_1,
                            list);
                    mylist.setAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
        MyTask myTask = new MyTask();
        myTask.setHandler(handler);
        Thread t = new Thread(myTask);
        t.start();

    }
}